package chamber.n01.prime;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * Created by Gerardo on 2/8/14.
 */
public class PrimeImpl implements Prime {
	@Override
	public boolean apply(final Long input) {
		checkArgument(input > 0, "input must be positive");

		boolean couldBePrime = isOdd(input) || isTwo(input);
		for (long i = 3; couldBePrime && i <= Math.sqrt(input); i += 2) {
			couldBePrime = (input % i) != 0;
		}
		return couldBePrime;
	}

	private boolean isTwo(final Long input) {
		return input == 2;
	}

	private boolean isOdd(final Long input) {
		return (input & 1) != 0;
	}
}
